package se.hrbustrc.hlgshopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import se.hrbustrc.hlgshopping.config.RedisConfig;
import se.hrbustrc.hlgshopping.dto.LoginRequest;
import se.hrbustrc.hlgshopping.dto.LoginResponse;
import se.hrbustrc.hlgshopping.entity.User;
import se.hrbustrc.hlgshopping.mapper.UserMapper;
import se.hrbustrc.hlgshopping.service.UserService;
import se.hrbustrc.hlgshopping.utils.JwtUtils;
import se.hrbustrc.hlgshopping.utils.RedisUtils;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public User getUserByCode(String userCode) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fUserCode", userCode);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public LoginResponse login(LoginRequest loginRequest) {
        //先从缓存中搜索没有再找数据库





        User user = getUserByCode(loginRequest.getUsername());
        if(null == user){
            throw new RuntimeException("用户不存在");
        }
        if(!passwordEncoder.matches(loginRequest.getPassword(), user.getUserPass())){
            throw new RuntimeException("密码错误");
        }
        //生成token
        String token = jwtUtils.generateToken(user.getUserId(),user.getUserCode());
        //生成刷新缓存token
        String refreshToken = jwtUtils.generateRefreshToken(user.getUserId(),user.getUserCode());
        //将token 和用户信息 加入 redius缓存

        redisUtils.setUserToken(user.getUserCode(), token);
        LoginResponse result = new LoginResponse();
        result.setToken(token);
        result.setRefreshToken(refreshToken);
        result.setUsername(user.getUserName());
        result.setUserId((long)user.getUserId());
        return result;
    }

    @Override
    public boolean logout(String token) {
        if(jwtUtils.validateToken(token) && !jwtUtils.isTokenExpired(token)){
            String userCode = jwtUtils.getUsernameFromToken(token);
            redisUtils.deleteUserToken(userCode);
        }
        return false;
    }
    @Override
    public boolean validateToken(String token) {
        return jwtUtils.validateToken(token) && !jwtUtils.isTokenExpired(token);
    }
    @Override
    public User getUserByUsername(String username){
        return userMapper.findByUsername(username);
    }

}
